rm(list=ls())

##############################################################################
# TABLE 2 SCRIPT
##############################################################################

# load packages
#library(texreg)
#library(mice)
#library(MASS)

# load data 
load("data/eurepoc_year_attacks.RData")
load("data/zoneh_year_data.RData")
load("data/imputed_data_year.RData")

##############
# RUN MODELS #
##############

#model 1 (eurepoc attacks no control)
m1 <- lm(attacks ~ treat + factor(country_spell), data = eurepoc_year_data)
#summary(m1)

#model 2 (eurepoc attacks control)
d.long$log_tech_articles <- log(d.long$tech_articles + 1)

imp.candidates <- as.mids(d.long)

fit.A <- with(imp.candidates, lm(attacks ~ treat + factor(country_spell) +
                                   internet_usage + ict_exports + log_tech_articles))
m2 <- pool(fit.A)
#summary(m2)

#model 3 (zoneh attacks)
zoneh_year_data$treat <- zoneh_year_data$election
m3 <- glm.nb(attacks ~ election + factor(group_spell),
             data = zoneh_year_data, link = log, control=glm.control(maxit=50))
#summary(m3)

################
# CREATE TABLE #
################

note <- c("Model 1 and 2 OLS. Model 3 negative binomial.")

tab2 <- screenreg(list(m1, m2, m3), custom.coef.map = list("election" = "candidate",
                                                   "treat" = "candidate", 
"internet_usage" = "internet usage", 
"ict_exports" = "ICT exports", 
"log_tech_articles" = "log tech articles"),
 omit.coef = c("country_spell"), custom.note = note, custom.model.names = c("EuRepoC", "EuRepoC", "Zone-H"), 
custom.gof.rows = list("Country Spell FE" = c("YES", "YES", "NO"), "Group Spell FE" = c("NO", "NO", "YES")), digits = 3, stars = c(0.001, 0.01, 0.05, 0.1))

print(tab2)

# script complete message 
print("tab2 complete")